﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;


public class ManageDriverControl
{
    /// <summary>
    /// Lấy danh sách tất cả các tài xế
    /// </summary>
    /// <returns></returns>
    public DataSet getAllDrivers()
    {
        DataSet ds = new DataSet();
        SqlConnection con = ManageConnect.OpenConnection();
        try
        {
            SqlDataAdapter da = new SqlDataAdapter("getAllDriver", con.ConnectionString);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.Fill(ds, "Drivers");
        }
        catch { }
        finally
        {
            ManageConnect.CloseConnection(con);
        }

        return ds;
    }

    public DataSet SearchDriversByKeyword(string key)
    {
        DataSet ds = new DataSet();
        SqlConnection con = ManageConnect.OpenConnection();
        try
        {
            SqlDataAdapter sqlDb = new SqlDataAdapter("searchDriverByName", con);
            sqlDb.SelectCommand.CommandType = CommandType.StoredProcedure;

            SqlParameter param = new SqlParameter("@STRING_SEARCH", SqlDbType.NVarChar);
            param.Value = key;
            sqlDb.SelectCommand.Parameters.Add(param);

            sqlDb.Fill(ds, "Drivers");
        }
        catch (SqlException ex)
        {
            /* ghi ra file log*/

        }
        finally
        {
            ManageConnect.CloseConnection(con);
        }

        return ds;
    }

    public int addNewDriver(Driver driver)
    {
        SqlConnection con = ManageConnect.OpenConnection();

        SqlCommand cmd = new SqlCommand("insertNewDriver", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@FullName", SqlDbType.NVarChar).Value = driver.FullName;
        cmd.Parameters.Add("@DateOfBirth", SqlDbType.Date).Value = driver.DateOfBirth;
        cmd.Parameters.Add("@RankDriver", SqlDbType.Int).Value = driver.RankDriver;
        cmd.Parameters.Add("@Nationality", SqlDbType.NVarChar).Value = driver.Nationality;
        cmd.Parameters.Add("@Residence", SqlDbType.NVarChar).Value = driver.Residence;
        cmd.Parameters.Add("@TimeOut", SqlDbType.Date).Value = driver.TimeOut;
        cmd.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = driver.Phone;
        cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = driver.Email;
        cmd.Parameters.Add("@IDNumber", SqlDbType.Int).Value = driver.IDNumber;
        cmd.Parameters.Add("@AvarImage", SqlDbType.NVarChar).Value = driver.AvaImage;
        SqlParameter result = new SqlParameter("@ReturnValue", SqlDbType.Int);
        result.Direction = ParameterDirection.ReturnValue;
        cmd.Parameters.Add(result);

        try
        {
            cmd.ExecuteNonQuery();
            int val;
            if (int.TryParse(result.Value.ToString(), out val))
            {
                return val;
            }
            else return -1;
        }
        catch (SqlException ex)
        {
            /* ghi ra file log*/
            return -1;
        }
        finally
        {
            ManageConnect.CloseConnection(con);
        }
    }

    public int deleteDriver(Driver driver)
    {
        SqlConnection con = ManageConnect.OpenConnection();

        SqlCommand cmd = new SqlCommand("deleteDriverByID", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@IDDriver", SqlDbType.Int).Value = driver.ID;

        SqlParameter result = new SqlParameter("@ReturnValue", SqlDbType.Int);
        result.Direction = ParameterDirection.ReturnValue;
        cmd.Parameters.Add(result);

        try
        {
            cmd.ExecuteNonQuery();
            int val;
            if (int.TryParse(result.Value.ToString(), out val))
            {
                return val;
            }
            else return -1;
        }
        catch (SqlException ex)
        {
            /* ghi ra file log*/
            return -1;
        }
        finally
        {
            ManageConnect.CloseConnection(con);
        }
    }

    public int editDriver(Driver driver)
    {
        SqlConnection con = ManageConnect.OpenConnection();

        SqlCommand cmd = new SqlCommand("updateDriver", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = driver.ID;
        cmd.Parameters.Add("@FullName", SqlDbType.NVarChar).Value = driver.FullName;
        cmd.Parameters.Add("@DateOfBirth", SqlDbType.Date).Value = driver.DateOfBirth;
        cmd.Parameters.Add("@RankDriver", SqlDbType.Int).Value = driver.RankDriver;
        cmd.Parameters.Add("@Nationality", SqlDbType.NVarChar).Value = driver.Nationality;
        cmd.Parameters.Add("@Residence", SqlDbType.NVarChar).Value = driver.Residence;
        cmd.Parameters.Add("@TimeOut", SqlDbType.Date).Value = driver.TimeOut;
        cmd.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = driver.Phone;
        cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = driver.Email;
        cmd.Parameters.Add("@IDNumber", SqlDbType.Int).Value = driver.IDNumber;
        cmd.Parameters.Add("@AvarImage", SqlDbType.NVarChar).Value = "Empty";
        SqlParameter result = new SqlParameter("@ReturnValue", SqlDbType.Int);
        result.Direction = ParameterDirection.ReturnValue;
        cmd.Parameters.Add(result);

        try
        {
            cmd.ExecuteNonQuery();
            int val;
            if (int.TryParse(result.Value.ToString(), out val))
            {
                return val;
            }
            else return -1;
        }
        catch (SqlException ex)
        {
            /* ghi ra file log*/
            return -1;
        }
        finally
        {
            ManageConnect.CloseConnection(con);
        }
    }
}
